home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / shutdown / shutdown.c < prev    next >
C/C++ Source or Header  |  1990-07-10  |  5KB  |  208 lines

  1. /* 
  2.  * shutdown.c --
  3.  *
  4.  *    Program to shutdown the operating system.
  5.  *
  6.  * Copyright (C) 1988 Regents of the University of California
  7.  * All rights reserved.
  8.  */
  9.  
  10. #ifndef lint
  11. static char rcsid[] = "$Header: /sprite/src/cmds/shutdown/RCS/shutdown.c,v 1.11 90/01/24 07:46:05 douglis Exp Locker: jhh $ SP RITE (Berkeley)";
  12. #endif not lint
  13.  
  14. #include "sys.h"
  15. #include "option.h"
  16. #include <stdio.h>
  17. #include <sys/file.h>
  18. #include <errno.h>
  19.  
  20. #define FASTBOOT "/local/fastboot"
  21.  
  22. /*
  23.  * Options.
  24.  */
  25. static int    halt = 0;        /* Non-zero means halt. */
  26. static int    dontSyncDisks = 0;    /* Non-zero means don't sync the disks
  27.                      * when shutting down the system. */
  28. static int    reboot = 0;        /* Non-zero means reboot. */
  29. static int    debug = 0;        /* Non-zero means enter the debugger. */
  30. static int    fastBoot = 0;        /* Non-zero means don't check the disks
  31.                      * on reboot. */
  32. static int    quickBoot = 0;        /* Whether not to do wall. */
  33. static int    sleepTime = 30;        /* Number of seconds to sleep after
  34.                      * wall. */
  35. static int    singleUser = 0;        /* Non-zero means reboot single user. */
  36. static int    client    = 0;        /* Non-zero means reboot fileserver
  37.                      * without using /boot on local disk. */
  38. static int    rootcmds = 0;        /* Non-zero means run rootcmds. */
  39. static int    debugShutdown = 0;    /* Non-zero means don't really
  40.                      * shut down. */
  41. static int    root = 0;        /* Non-zero means reboot as root. */
  42. static int    nonroot = 0;        /* Non-zero means don't reboot as 
  43.                      * root. */
  44. static char    *rootdisk = NULL;    /* Disk to attach during boot. */
  45. /*
  46.  * String to use when rebooting the system.
  47.  */
  48. char        nullString[] = "";
  49. char        *rebootString = nullString;
  50. char        buffer[100];
  51.  
  52. /*
  53.  * Flags to command-line options:
  54.  */
  55. Option optionArray[] = {
  56.     {OPT_TRUE, "h", (Address) &halt, "Halt (This is the default)"},
  57.     {OPT_TRUE, "r", (Address) &reboot, "Reboot"},
  58.     {OPT_STRING, "R", (Address) &rebootString, "String to pass to boot prom (implies -r)"},
  59.     {OPT_TRUE, "d", (Address) &debug, "Enter the debugger"},
  60.     {OPT_TRUE, "f", (Address) &fastBoot,
  61.          "Don't check disk consistency upon reboot (reboot if no other options.)"},
  62.     {OPT_TRUE, "w", (Address) &dontSyncDisks, "Dont write back the cache (Default is to write it back)"},
  63.     {OPT_TRUE, "s", (Address) &singleUser, "Reboot single user mode"},
  64.     {OPT_TRUE, "c", (Address) &client, 
  65.     "Reboot fileserver as a client (don't use /boot on local disk)"},
  66.     {OPT_TRUE, "x", (Address) &rootcmds, "Run rootcmds before diskcmds"},
  67.     {OPT_TRUE, "q", (Address) &quickBoot, "Don't do a wall, and wait, before rebooting"},
  68.     {OPT_INT, "S", (Address) &sleepTime, "Number of seconds to wait after wall"},
  69.     {OPT_TRUE, "D", (Address) &debugShutdown, "Don't actually shut down"},
  70.     {OPT_TRUE, "root", (Address) &root, "Reboot as root server"},
  71.     {OPT_TRUE, "nonroot", (Address) &nonroot, "Don't reboot as root server"},
  72.     {OPT_STRING, "rootdisk", (Address) &rootdisk, "Disk to attach during boot"},
  73. };
  74. int numOptions = sizeof(optionArray) / sizeof(Option);
  75.  
  76.  
  77. /*
  78.  *----------------------------------------------------------------------
  79.  *
  80.  * main --
  81.  *
  82.  *    The main program for shutdown.
  83.  *
  84.  * Results:
  85.  *    None.
  86.  *
  87.  * Side effects:
  88.  *    Prints information on standard output.
  89.  *
  90.  *----------------------------------------------------------------------
  91.  */
  92. main(argc, argv)
  93.     int argc;
  94.     char *argv[];
  95. {
  96.     int    flags;
  97.  
  98.     (void) Opt_Parse(argc, argv, optionArray, numOptions, OPT_ALLOW_CLUSTERING);
  99.     if (!quickBoot) {
  100.     char *args[3];
  101.     int fd[2];
  102.     int pid;
  103.     
  104.     sprintf(buffer, "system shutting down in %d seconds\n", sleepTime);
  105.     if (pipe(fd) < 0) {
  106.         perror("pipe");
  107.         exit(1);
  108.     }
  109.     if (write(fd[1], buffer, strlen(buffer) + 1) < 0) {
  110.         perror("write");
  111.         exit(1);
  112.     }
  113.     close(fd[1]);
  114.     pid = fork();
  115.     if (pid < 0) {
  116.         perror("fork");
  117.         exit(1);
  118.     }
  119.     if (pid == 0) {
  120.         args[0] = "wall";
  121.         args[1] = "-l";
  122.         args[2] = 0;
  123.         if (dup2(fd[0], 0) < 0) {
  124.         perror("dup2");
  125.         exit(0);
  126.         }
  127.         execv("/sprite/cmds/wall", args);
  128.         perror("execv");
  129.     } else {
  130.         wait(0);
  131.         sleep(sleepTime);
  132.     }
  133.     }
  134.     strncpy(buffer, rebootString, 100);
  135.     if (strlen(rebootString) > 0) {
  136.     reboot = 1;
  137.     }
  138.     if (dontSyncDisks) {
  139.     flags = 0;
  140.     } else {
  141.     flags = SYS_WRITE_BACK;
  142.     }
  143.     if (fastBoot) {
  144.     char    str[80];
  145.     int    fd;
  146.  
  147.     fd = open(FASTBOOT, O_CREAT, 0777);
  148.     if (fd < 0) {
  149.         if (errno != ENOENT) {
  150.         sprintf(str, "Couldn't open %s", FASTBOOT);
  151.         perror(str);
  152.         exit(1);
  153.         }
  154.     }
  155.  
  156.     strcat(buffer, " -f");
  157.     if (!halt && !debug) {
  158.         reboot = 1;
  159.     }
  160.     }
  161.     if (singleUser) {
  162.     strcat(buffer, " -s");
  163.     if (!halt && !debug) {
  164.         reboot = 1;
  165.     }
  166.     }
  167.     if (client) {
  168.     strcat(buffer, " -c");
  169.     if (!halt && !debug) {
  170.         reboot = 1;
  171.     }
  172.     }
  173.     if (rootcmds) {
  174.     strcat(buffer, " -x");
  175.     if (!halt && !debug) {
  176.         reboot = 1;
  177.     }
  178.     }
  179.     if (root) {
  180.     strcat(buffer, " -root");
  181.     if (!halt && !debug) {
  182.         reboot = 1;
  183.     }
  184.     }
  185.     if (nonroot) {
  186.     strcat(buffer, " -nonroot");
  187.     if (!halt && !debug) {
  188.         reboot = 1;
  189.     }
  190.     }
  191.     if (rootdisk) {
  192.     strcat(buffer, " -rootdisk ");
  193.     strcat(buffer, rootdisk);
  194.     if (!halt && !debug) {
  195.         reboot = 1;
  196.     }
  197.     }
  198.     if (debugShutdown) {
  199.     fprintf(stderr, "Would call Sys_Shutdown(%s) here.\n", reboot ? buffer : "");
  200.     } else if (debug) {
  201.     Sys_Shutdown(flags | SYS_KILL_PROCESSES | SYS_DEBUG, NULL);
  202.     } else if (reboot) { 
  203.     Sys_Shutdown(flags | SYS_KILL_PROCESSES | SYS_REBOOT, buffer);
  204.     } else {
  205.     Sys_Shutdown(flags | SYS_KILL_PROCESSES | SYS_HALT, NULL);
  206.     }
  207. }
  208.